package com.crm.service;

import com.crm.entity.Storage;
import com.crm.repository.StorageRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("storageService")
public class StorageServiceImpl implements StorageService {

    @Resource
    StorageRepository storageRepository;

    @Override
    public Page<Storage> findAllByPage(Pageable pageable, String stkWarehouse, String prodName) {
        /*System.out.println(stkWarehouse + " 33 " + prodName);
        Specification<Storage> specification = new Specification<Storage>() {
            @Override
            public Predicate toPredicate(Root<Storage> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (stkWarehouse != null && !stkWarehouse.equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("stkWarehouse"), "%" + stkWarehouse + "%"));
                }
                if (prodName != null && !prodName.equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("product.prodName"), "%" + prodName + "%"));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return storageRepository.findAll(specification, pageable);*/
        return storageRepository.findByStkWarehouseLikeAndProduct_ProdNameLike(pageable,
                "%" + stkWarehouse + "%", "%" + prodName + "%");
    }

    @Override
    public List<Storage> findAll() {
        return storageRepository.findAll();
    }
}
